﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace Suzsoft.Smart.Data
{
    public class ConnConfigurationManager
    {
        private ConnConfiguration connConfigurationSelected;
        private List<ConnConfiguration> connConfigurations = new List<ConnConfiguration>();
        private CustomDesEncryption customDesEncryption = new CustomDesEncryption();

        #region ctor
        public ConnConfigurationManager()
        {

        }
        #endregion

        public List<ConnConfiguration> GetConfigurations()
        {
            connConfigurations = new List<ConnConfiguration>();
            if (File.Exists(Application.StartupPath + "\\cfg.xml"))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Application.StartupPath + "\\cfg.xml");
                XmlNodeList xmlNodes = xmlDoc.SelectNodes("//root//LogonInfo");
                foreach (XmlNode xmlNode in xmlNodes)
                {
                    ConnConfiguration connConfiguration = new ConnConfiguration();
                    connConfiguration.ConfigName = xmlNode.Attributes["ConfigName"].Value;
                    connConfiguration.DBType = xmlNode.SelectSingleNode("DBType").InnerText;
                    connConfiguration.DBAddress = xmlNode.SelectSingleNode("DBAddress").InnerText;
                    connConfiguration.DBName = xmlNode.SelectSingleNode("DBName").InnerText;
                    connConfiguration.DBUser = xmlNode.SelectSingleNode("DBUser").InnerText;
                    connConfiguration.DBEncryptedPwd = xmlNode.SelectSingleNode("DBPwd").InnerText;
                    connConfiguration.ServerURL = xmlNode.SelectSingleNode("ServerURL").InnerText;
                    if (xmlNode.Attributes["SelectedTag"].Value.Equals("Selected"))
                    {
                        connConfiguration.IsApply = true;
                    }
                    else
                    {
                        connConfiguration.IsApply = false;
                    }
                    connConfigurations.Add(connConfiguration);
                }
            }
            return connConfigurations;
        }

        public ConnConfiguration GetConfigurationSelected(int nIndex)
        {
            if (connConfigurations.Count == 0)
            {
                return null;
            }
            return connConfigurations[nIndex];
        }

        private bool CreateFile(ConnConfiguration connConfiguration)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmlDoc.AppendChild(xmlDeclaration);
                XmlElement xeRoot = xmlDoc.CreateElement("root");
                XmlElement xeFirstNode = xmlDoc.CreateElement("LogonInfo");
                xeFirstNode.SetAttribute("ConfigName", connConfiguration.ConfigName);
                xeFirstNode.SetAttribute("SelectedTag", "");
                XmlElement xeDBType = xmlDoc.CreateElement("DBType");
                XmlElement xeDBAddress = xmlDoc.CreateElement("DBAddress");
                XmlElement xeDBName = xmlDoc.CreateElement("DBName");
                XmlElement xeDBUser = xmlDoc.CreateElement("DBUser");
                XmlElement xeDBPwd = xmlDoc.CreateElement("DBPwd");
                XmlElement xeServerURL = xmlDoc.CreateElement("ServerURL");
                xmlDoc.AppendChild(xeRoot);
                xeRoot.AppendChild(xeFirstNode);
                xeFirstNode.AppendChild(xeDBType);
                xeFirstNode.AppendChild(xeDBAddress);
                xeFirstNode.AppendChild(xeDBName);
                xeFirstNode.AppendChild(xeDBUser);
                xeFirstNode.AppendChild(xeDBPwd);
                xeFirstNode.AppendChild(xeServerURL);
                xeDBType.InnerText = connConfiguration.DBType;
                xeDBAddress.InnerText = connConfiguration.DBAddress;
                xeDBName.InnerText = connConfiguration.DBName;
                xeDBUser.InnerText = connConfiguration.DBUser;
                xeDBPwd.InnerText = customDesEncryption.EncryptString(connConfiguration.DBEncryptedPwd);
                xeServerURL.InnerXml = connConfiguration.ServerURL;
                xmlDoc.Save(Application.StartupPath + "\\cfg.xml");
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private bool NewConfiguration(ConnConfiguration connConfiguration)
        {
            if (!File.Exists(Application.StartupPath + "\\cfg.xml"))
            {
                if (CreateFile(connConfiguration) == false)
                {
                    return false;
                }
            }
            else
            {
                try
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(Application.StartupPath + "\\cfg.xml");
                    XmlElement xeRoot = (XmlElement)xmlDoc.GetElementsByTagName("root")[0];
                    XmlElement xeLogonInfo = xmlDoc.CreateElement("LogonInfo");
                    XmlElement xeDBType = xmlDoc.CreateElement("DBType");
                    XmlElement xeDBAddress = xmlDoc.CreateElement("DBAddress");
                    XmlElement xeDBName = xmlDoc.CreateElement("DBName");
                    XmlElement xeDBUser = xmlDoc.CreateElement("DBUser");
                    XmlElement xeDBPwd = xmlDoc.CreateElement("DBPwd");
                    XmlElement xeServerURL = xmlDoc.CreateElement("ServerURL");
                    xeRoot.AppendChild(xeLogonInfo);
                    xeLogonInfo.SetAttribute("ConfigName", connConfiguration.ConfigName);
                    xeLogonInfo.SetAttribute("SelectedTag", "");
                    xeLogonInfo.AppendChild(xeDBType);
                    xeLogonInfo.AppendChild(xeDBAddress);
                    xeLogonInfo.AppendChild(xeDBName);
                    xeLogonInfo.AppendChild(xeDBUser);
                    xeLogonInfo.AppendChild(xeDBPwd);
                    xeLogonInfo.AppendChild(xeServerURL);
                    XmlNode xmlNode = xmlDoc.SelectSingleNode("//root//LogonInfo[@ConfigName='" + connConfiguration.ConfigName + "']");
                    xmlNode.SelectSingleNode("DBType").InnerText = connConfiguration.DBType;
                    xmlNode.SelectSingleNode("DBAddress").InnerText = connConfiguration.DBAddress;
                    xmlNode.SelectSingleNode("DBName").InnerText = connConfiguration.DBName;
                    xmlNode.SelectSingleNode("DBUser").InnerText = connConfiguration.DBUser;
                    xmlNode.SelectSingleNode("DBPwd").InnerText = customDesEncryption.EncryptString(connConfiguration.DBEncryptedPwd);
                    xmlNode.SelectSingleNode("ServerURL").InnerText = connConfiguration.ServerURL;
                    xmlDoc.Save(Application.StartupPath + "\\cfg.xml");
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return true;
        }

        private bool ModifyConfiguration(ConnConfiguration connConfiguration)
        {
            try
            {
                if (!File.Exists(Application.StartupPath + "\\cfg.xml"))
                {
                    return false;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Application.StartupPath + "\\cfg.xml");
                XmlNode xmlNode = xmlDoc.SelectSingleNode("//root//LogonInfo[@ConfigName='" + connConfiguration.ConfigName + "']");
                if (xmlNode == null)
                {
                    return false;
                }
                xmlNode.SelectSingleNode("DBType").InnerText = connConfiguration.DBType;
                xmlNode.SelectSingleNode("DBAddress").InnerText = connConfiguration.DBAddress;
                xmlNode.SelectSingleNode("DBName").InnerText = connConfiguration.DBName;
                xmlNode.SelectSingleNode("DBUser").InnerText = connConfiguration.DBUser;
                xmlNode.SelectSingleNode("DBPwd").InnerText = customDesEncryption.EncryptString(connConfiguration.DBEncryptedPwd);
                xmlNode.SelectSingleNode("ServerURL").InnerText = connConfiguration.ServerURL;
                xmlDoc.Save(Application.StartupPath + "\\cfg.xml");
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private bool DeleteConfiguration(ConnConfiguration connConfiguration)
        {
            try
            {
                if (!File.Exists(Application.StartupPath + "\\cfg.xml"))
                {
                    return false;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Application.StartupPath + "\\cfg.xml");
                XmlNode xmlNode = xmlDoc.SelectSingleNode("//root//LogonInfo[@ConfigName='" + connConfiguration.ConfigName + "']");
                if (xmlNode == null)
                {
                    return false;
                }
                xmlDoc.SelectSingleNode("//root").RemoveChild(xmlNode);
                xmlDoc.Save(Application.StartupPath + "\\cfg.xml");
                ConnConfigurationSelected.ConfigName = null;
                ConnConfigurationSelected.DBType = null;
                ConnConfigurationSelected.DBAddress = null;
                ConnConfigurationSelected.DBName = null;
                ConnConfigurationSelected.DBUser = null;
                ConnConfigurationSelected.DBPwd = null;
                ConnConfigurationSelected.ServerURL = null;
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool SaveConfiguration(ConnConfiguration connConfiguration, CrudFlag crudFlag)
        {
            if (crudFlag == CrudFlag.新增)
            {
                if (NewConfiguration(connConfiguration) == false)
                {
                    return false;
                }
            }
            else if (crudFlag == CrudFlag.修改)
            {
                if (ModifyConfiguration(connConfiguration) == false)
                {
                    return false;
                }
            }
            else if (crudFlag == CrudFlag.删除)
            {
                if (DeleteConfiguration(connConfiguration) == false)
                {
                    return false;
                }
            }
            return true;
        }

        public bool ApplyConfiguration(ConnConfiguration connConfiguration)
        {
            try
            {
                if (!File.Exists(Application.StartupPath + "\\cfg.xml"))
                {
                    return false;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Application.StartupPath + "\\cfg.xml");
                XmlNode xmlNode = xmlDoc.SelectSingleNode("//root//LogonInfo[@ConfigName='" + connConfiguration.ConfigName + "']");
                if (xmlNode == null)
                {
                    return false;
                }
                XmlNode xmlNodeSelected = xmlDoc.SelectSingleNode("//root//LogonInfo[@SelectedTag='Selected']");
                if (xmlNodeSelected != null)
                {
                    ((XmlElement)xmlNodeSelected).SetAttribute("SelectedTag", "");
                }
                ((XmlElement)xmlNode).SetAttribute("SelectedTag", "Selected");
                xmlDoc.Save(Application.StartupPath + "\\cfg.xml");
                ConnConfigurationSelected.ConfigName = connConfiguration.ConfigName;
                ConnConfigurationSelected.DBType = connConfiguration.DBType;
                ConnConfigurationSelected.DBAddress = connConfiguration.DBAddress;
                ConnConfigurationSelected.DBName = connConfiguration.DBName;
                ConnConfigurationSelected.DBUser = connConfiguration.DBUser;
                ConnConfigurationSelected.DBPwd = connConfiguration.DBUnencryptedPwd;
                ConnConfigurationSelected.ServerURL = connConfiguration.ServerURL;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool InitConfiguration()
        {
            try
            {
                if (!File.Exists(Application.StartupPath + "\\cfg.xml"))
                {
                    return false;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Application.StartupPath + "\\cfg.xml");
                XmlNode xmlNodeSelected = xmlDoc.SelectSingleNode("//root//LogonInfo[@SelectedTag='Selected']");
                if (xmlNodeSelected != null)
                {
                    ConnConfigurationSelected.ConfigName = xmlNodeSelected.Attributes["ConfigName"].Value;
                    ConnConfigurationSelected.DBType = xmlNodeSelected.SelectSingleNode("DBType").InnerText;
                    ConnConfigurationSelected.DBAddress = xmlNodeSelected.SelectSingleNode("DBAddress").InnerText;
                    ConnConfigurationSelected.DBName = xmlNodeSelected.SelectSingleNode("DBName").InnerText;
                    ConnConfigurationSelected.DBUser = xmlNodeSelected.SelectSingleNode("DBUser").InnerText;
                    string encryption = xmlNodeSelected.SelectSingleNode("DBPwd").InnerText;
                    CustomDesEncryption customDesEncryption = new CustomDesEncryption();
                    ConnConfigurationSelected.DBPwd = customDesEncryption.DecryptString(encryption);
                    ConnConfigurationSelected.ServerURL = xmlNodeSelected.SelectSingleNode("ServerURL").InnerText;
                }
                else
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
